# -*- coding: utf-8 -*-
# 两品种或两合约套利策略
from tool.gongju import *


def 两合约套利_v1(cl_sj):
    """
    套利系统，套利方向可调，
    套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
    一：开仓逻辑：
    当IC 主力合约-次主力合约大于n点后开仓，哪个合约先成交，另外一个n秒后撤单追单，两个都成交后进行下一轮
    """
    # 数据初始化，
    shijian = int(time.strftime("%H%M%S"))  # 当前电脑时间
    zhbh = cl_sj.jiaoyishezhi[0][0]  # 开仓账户编号
    # 参数设置区
    ss = cl_sj.jiaoyishezhi[5][0]  # 每次开仓数量
    jiacha = cl_sj.jiaoyishezhi[5][1]  # 两合约价差
    zongshou = cl_sj.jiaoyishezhi[5][2]  # 一共开平多少手
    zdsj = cl_sj.jiaoyishezhi[5][3]  # 多少秒测单追单
    wtpy_czl = cl_sj.jiaoyishezhi[5][4]  # 次主力委托偏移
    czlhy = cl_sj.jiaoyishezhi[5][5]   # 另外一腿合约，第一腿为交易合约
    cjfx = cl_sj.jiaoyishezhi[5][6]  # 差价方向，0：差价扩大下单，1：差价缩小下单
    taolileixing = cl_sj.jiaoyishezhi[5][7]  # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平

    if 'zsl' not in cl_sj.sjb:  # 初始化时执行一次，用于初始化缓存字典
        cl_sj.sjb['zsl'] = zongshou
        cl_sj.sjb['xdcs'] = ss  # 下单次数
        cl_sj.sjb['cczt_zl'] = 0  # 主力持仓状态
        cl_sj.sjb['cczt_czl'] = 0  # 次主力持仓状态
        cl_sj.sjb['hydx'] = cl_sj.zhanghu[zhbh].get_zuixiaobiandong(cl_sj.jiaoyishezhi[3][0])   # 最小变动单位
        cl_sj.sjb['xdid_zl'] = '8888'  # 止盈挂单id
        cl_sj.sjb['xdid_czl'] = '8888'  # 止盈挂单id
        cl_sj.sjb['cckcsj_zl'] = time.time()
        cl_sj.sjb['cckcsj_czl'] = time.time()
        cl_sj.sjb['zhujiaoyiheyue'] = cl_sj.jiaoyishezhi[3][0]  # 主力合约
        cl_sj.sjb['wtpy_zl'] = cl_sj.jiaoyishezhi[8][0]  # 主力委托偏移

    zltick = DuquRedis_HQ(cl_sj.sjb['zhujiaoyiheyue'])
    zljg = zltick['LastPrice']
    czltick = DuquRedis_HQ(czlhy)
    czljg = czltick['LastPrice']
    hycj = zljg-czljg  # 两合约差价
    cckcbz = 0

    zhangdietingxianzhi = zljg < float(zltick['UpperLimitPrice']) and zljg > float(zltick['LowerLimitPrice']) and \
                          czljg < float(czltick['UpperLimitPrice']) and czljg > float(czltick['LowerLimitPrice'])

    yunxukaicang = (shijian > 90100 and shijian < 101400) or (shijian > 103100 and shijian < 112900) or (
                shijian > 130100 and shijian < 145800) \
                   or (shijian > 210100 and shijian < 235950) or (shijian > 0 and shijian < 22800)  # 允许开仓时间

    if cl_sj.jiaoyiqidong and zhangdietingxianzhi and yunxukaicang:  # 启动了交易且时间在开盘后10秒
        # 初次开仓
        if cjfx:  # 差价方向为1，差价缩小下单
            chajiatiaojian = hycj < jiacha
        else:  # 差价方向为0，差价扩大下单
            chajiatiaojian = hycj > jiacha
        if chajiatiaojian and cl_sj.sjb['cczt_zl'] == 0 and cl_sj.sjb['cczt_czl'] == 0 and cl_sj.sjb['zsl'] > 0:  # 合约差价大于设定价差
            cl_sj.jiaoyishezhi[3][0] = cl_sj.sjb['zhujiaoyiheyue']
            cl_sj.jiaoyishezhi[8][0] = cl_sj.sjb['wtpy_zl']  # 主力委托偏移
            # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平，7：平空-开空
            if taolileixing == 1:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力开多
            elif taolileixing == 2:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
            elif taolileixing == 3:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力开空
            elif taolileixing == 4:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力平空
            elif taolileixing == 5:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
            elif taolileixing == 6:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
            elif taolileixing == 7:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力平空

            print("主力发单平仓=", time.strftime("%H%M%S"), cl_sj.jiaoyishezhi[3][0])
            cl_sj.sjb['cczt_zl'] = 1
            cl_sj.sjb['cckcsj_zl'] = time.time()
            cl_sj.jiaoyishezhi[3][0] = czlhy
            cl_sj.jiaoyishezhi[8][0] = wtpy_czl  # 次主力委托偏移
            # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
            if taolileixing == 1:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力开空
            elif taolileixing == 2:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力平空
            elif taolileixing == 3:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力开多
            elif taolileixing == 4:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力平多
            elif taolileixing == 5:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力开多
            elif taolileixing == 6:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力平空
            elif taolileixing == 7:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力开空

            print("次主力发单开仓=", time.strftime("%H%M%S"), cl_sj.jiaoyishezhi[3][0])
            cl_sj.sjb['cczt_czl'] = 1
            cl_sj.sjb['cckcsj_czl'] = time.time()
            cl_sj.sjb['zsl'] = cl_sj.sjb['zsl'] - 1

            cckcbz = 1

        # 主力追单
        zlzdcgbz = True
        if cl_sj.sjb['cczt_zl'] == 1 and cckcbz == 0:  # 初次下单n秒不成交撤单并追单
            if cl_sj.sjb['xdid_zl'] in cl_sj.zhanghu[zhbh].order_dict:
                if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zl']]['kcdsl'] != 0:
                    if time.time() - cl_sj.sjb['cckcsj_zl'] > zdsj:  # 时间到达追单时间
                        cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_zl'])  # 撤初次挂单
                        time.sleep(0.3)
                        cl_sj.jiaoyishezhi[3][0] = cl_sj.sjb['zhujiaoyiheyue']
                        cl_sj.jiaoyishezhi[8][0] = cl_sj.sjb['wtpy_zl']  # 主力委托偏移
                        # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
                        if taolileixing == 1:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力开多
                        elif taolileixing == 2:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
                        elif taolileixing == 3:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力开空
                        elif taolileixing == 4:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力平空
                        elif taolileixing == 5:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
                        elif taolileixing == 6:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
                        elif taolileixing == 7:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力平空

                        cl_sj.sjb['cckcsj_zl'] = time.time()
                        print("主力追单=", time.strftime("%H%M%S"), cl_sj.jiaoyishezhi[3][0])
                        zlzdcgbz = False
                else:
                    if zlzdcgbz:
                        cl_sj.sjb['cczt_zl'] = 0  # 可撤单数量为0允许开仓

        # 次主力追单
        czlzdcgbz = True
        if cl_sj.sjb['cczt_czl'] == 1 and cckcbz == 0:  # 初次下单n秒不成交撤单并追单
            if cl_sj.sjb['xdid_czl'] in cl_sj.zhanghu[zhbh].order_dict:
                if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_czl']]['kcdsl'] != 0:
                    if time.time() - cl_sj.sjb['cckcsj_czl'] > zdsj:  # 时间到达追单时间
                        cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_czl'])  # 撤初次挂单
                        time.sleep(0.3)
                        cl_sj.jiaoyishezhi[3][0] = czlhy
                        cl_sj.jiaoyishezhi[8][0] = wtpy_czl  # 次主力委托偏移
                        # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
                        if taolileixing == 1:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力开空
                        elif taolileixing == 2:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力平空
                        elif taolileixing == 3:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力开多
                        elif taolileixing == 4:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力平多
                        elif taolileixing == 5:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力开多
                        elif taolileixing == 6:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力平空
                        elif taolileixing == 7:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力开空

                        cl_sj.sjb['cckcsj_czl'] = time.time()
                        print("次主力追单=", time.strftime("%H%M%S"), cl_sj.jiaoyishezhi[3][0])
                        czlzdcgbz = False
                else:
                    if czlzdcgbz:
                        cl_sj.sjb['cczt_czl'] = 0  # 可撤单数量为0允许开仓

    return cl_sj.sjb['cczt_zl'], cl_sj.sjb['cczt_czl'], cl_sj.sjb['zsl'], hycj


def 两合约套利_v2(cl_sj):
    """
    套利系统，套利方向可调，
    套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
    一：开仓逻辑：
    当IC 主力合约-次主力合约大于n点后开仓，哪个合约先成交，另外一个合约立马对价追单。
    """
    # 数据初始化，
    shijian = int(time.strftime("%H%M%S"))  # 当前电脑时间
    zhbh = cl_sj.jiaoyishezhi[0][0]  # 开仓账户编号
    # 参数设置区
    ss = cl_sj.jiaoyishezhi[5][0]  # 每次开仓数量
    jiacha = cl_sj.jiaoyishezhi[5][1]  # 两合约价差
    zongshou = cl_sj.jiaoyishezhi[5][2]  # 一共开平多少手
    zdsj = cl_sj.jiaoyishezhi[5][3]  # 一边成交后间隔多少秒检查一次是否追单，占时未用，为默认0.5秒检查一次
    wtpy_czl = cl_sj.jiaoyishezhi[5][4]  # 次主力委托偏移
    wtpy_zd = cl_sj.jiaoyishezhi[5][5]  # 追单委托偏移
    czlhy = cl_sj.jiaoyishezhi[5][6]  # 另外一腿合约，第一腿为交易合约
    cjfx = cl_sj.jiaoyishezhi[5][7]  # 差价方向，0：差价扩大下单，1：差价缩小下单
    taolileixing = cl_sj.jiaoyishezhi[5][8]  # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平

    if 'zsl' not in cl_sj.sjb:  # 初始化时执行一次，用于初始化缓存字典
        cl_sj.sjb['zsl'] = zongshou
        cl_sj.sjb['xdcs'] = ss  # 下单次数
        cl_sj.sjb['cczt_zl'] = 0  # 主力持仓状态
        cl_sj.sjb['cczt_czl'] = 0  # 次主力持仓状态
        cl_sj.sjb['hydx'] = cl_sj.zhanghu[zhbh].get_zuixiaobiandong(cl_sj.jiaoyishezhi[3][0])   # 最小变动单位
        cl_sj.sjb['xdid_zl'] = '8888'  # 止盈挂单id
        cl_sj.sjb['xdid_czl'] = '8888'  # 止盈挂单id
        cl_sj.sjb['cckcsj_zl'] = time.time()
        cl_sj.sjb['cckcsj_czl'] = time.time()
        cl_sj.sjb['zhujiaoyiheyue'] = cl_sj.jiaoyishezhi[3][0]  # 主力合约
        cl_sj.sjb['wtpy_zl'] = cl_sj.jiaoyishezhi[8][0]  # 主力委托偏移
        cl_sj.sjb['zdbz_zl'] = 0  # 追单标志用于追单时间间隔控制
        cl_sj.sjb['zdbz_czl'] = 0

    zltick = DuquRedis_HQ(cl_sj.sjb['zhujiaoyiheyue'])
    zljg = zltick['LastPrice']
    czltick = DuquRedis_HQ(czlhy)
    czljg = czltick['LastPrice']
    hycj = zljg-czljg  # 两合约差价

    zhangdietingxianzhi = zljg < float(zltick['UpperLimitPrice']) and zljg > float(zltick['LowerLimitPrice']) and \
                          czljg < float(czltick['UpperLimitPrice']) and czljg > float(czltick['LowerLimitPrice'])

    yunxukaicang = (shijian > 90100 and shijian < 101400) or (shijian > 103100 and shijian < 112900) or (
                shijian > 130100 and shijian < 145800) \
                   or (shijian > 210100 and shijian < 235950) or (shijian > 0 and shijian < 22800)  # 允许开仓时间

    cckcbz = True
    if cl_sj.jiaoyiqidong and yunxukaicang and zhangdietingxianzhi:  # 启动了交易且时间在开盘后10秒
        # 初次开仓
        if cjfx:  # 差价方向为1，差价缩小下单
            chajiatiaojian = hycj < jiacha
        else:  # 差价方向为0，差价扩大下单
            chajiatiaojian = hycj > jiacha
        if chajiatiaojian and cl_sj.sjb['cczt_zl'] == 0 and cl_sj.sjb['cczt_czl'] == 0 and cl_sj.sjb['zsl'] > 0:  # 合约差价大于设定价差
            cl_sj.jiaoyishezhi[3][0] = cl_sj.sjb['zhujiaoyiheyue']
            cl_sj.jiaoyishezhi[8][0] = cl_sj.sjb['wtpy_zl']  # 主力委托偏移
            # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
            if taolileixing == 1:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力开多
            elif taolileixing == 2:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
            elif taolileixing == 3:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力开空
            elif taolileixing == 4:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力平空
            elif taolileixing == 5:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
            elif taolileixing == 6:
                cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多

            print("主力发单平仓=", time.strftime("%H%M%S"), cl_sj.jiaoyishezhi[3][0])
            cl_sj.sjb['cczt_zl'] = 1
            cl_sj.sjb['cckcsj_zl'] = time.time()
            cl_sj.jiaoyishezhi[3][0] = czlhy
            cl_sj.jiaoyishezhi[8][0] = wtpy_czl  # 次主力委托偏移
            # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
            if taolileixing == 1:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力开空
            elif taolileixing == 2:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力平空
            elif taolileixing == 3:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力开多
            elif taolileixing == 4:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力平多
            elif taolileixing == 5:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力开多
            elif taolileixing == 6:
                cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力平空

            print("次主力发单开仓=", time.strftime("%H%M%S"), cl_sj.jiaoyishezhi[3][0])
            cl_sj.sjb['cczt_czl'] = 1
            cl_sj.sjb['cckcsj_czl'] = time.time()
            cl_sj.sjb['zsl'] = cl_sj.sjb['zsl'] - 1
            cckcbz = False

        # 主力追单
        zhuidanshijian = 0 if cl_sj.sjb['zdbz_zl'] == 0 else zdsj
        zlzdcgbz = True
        if cl_sj.sjb['cczt_zl'] == 1 and cckcbz:  # 次主力成交主力立马追单
            if cl_sj.sjb['xdid_zl'] in cl_sj.zhanghu[zhbh].order_dict and\
                    cl_sj.sjb['xdid_czl'] in cl_sj.zhanghu[zhbh].order_dict:
                if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zl']]['kcdsl'] != 0 and\
                        cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_czl']]['kcdsl'] == 0:
                    if time.time() - cl_sj.sjb['cckcsj_zl'] > zhuidanshijian:  # 时间到达追单时间间隔
                        cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_zl'])  # 撤初次挂单
                        time.sleep(0.3)
                        cl_sj.jiaoyishezhi[3][0] = cl_sj.sjb['zhujiaoyiheyue']
                        cl_sj.jiaoyishezhi[8][0] = wtpy_zd  # 追单委托偏移
                        # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
                        if taolileixing == 1:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力开多
                        elif taolileixing == 2:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
                        elif taolileixing == 3:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力开空
                        elif taolileixing == 4:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 主力平空
                        elif taolileixing == 5:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多
                        elif taolileixing == 6:
                            cl_sj.sjb['xdid_zl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 主力平多

                        print("主力追单=", time.strftime("%H%M%S"), cl_sj.jiaoyishezhi[3][0])
                        cl_sj.sjb['cckcsj_zl'] = time.time()
                        cl_sj.sjb['zdbz_zl'] = cl_sj.sjb['zdbz_zl']+1
                        zlzdcgbz = False

        # 次主力追单
        zhuidanshijian = 0 if cl_sj.sjb['zdbz_czl'] == 0 else zdsj
        czlzdcgbz = True
        if cl_sj.sjb['cczt_czl'] == 1 and cckcbz and zlzdcgbz:  # 主力成交次主力立马追单
            if cl_sj.sjb['xdid_czl'] in cl_sj.zhanghu[zhbh].order_dict and\
                    cl_sj.sjb['xdid_zl'] in cl_sj.zhanghu[zhbh].order_dict:
                if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_czl']]['kcdsl'] != 0 and\
                        cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zl']]['kcdsl'] == 0:
                    if time.time() - cl_sj.sjb['cckcsj_czl'] > zhuidanshijian:  # 时间到达追单时间间隔
                        cl_sj.zhanghu[zhbh].cd_orders(cl_sj.sjb['xdid_czl'])  # 撤初次挂单
                        time.sleep(0.2)
                        cl_sj.jiaoyishezhi[3][0] = czlhy
                        cl_sj.jiaoyishezhi[8][0] = wtpy_zd  # 次主力委托偏移
                        # 套利类型，1：买开-卖开，2：卖平-买平，3：卖开-买开，4：买平-卖平，5：卖平-买开，6：卖平-买平
                        if taolileixing == 1:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力开空
                        elif taolileixing == 2:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力平空
                        elif taolileixing == 3:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力开多
                        elif taolileixing == 4:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力平多
                        elif taolileixing == 5:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].kc_orders(cl_sj.jiaoyishezhi, 1, ss)  # 次主力开多
                        elif taolileixing == 6:
                            cl_sj.sjb['xdid_czl'] = cl_sj.zhanghu[zhbh].pc_orders(cl_sj.jiaoyishezhi, -1, ss)  # 次主力平空

                        print("次主力追单=", time.strftime("%H%M%S"), cl_sj.jiaoyishezhi[3][0])
                        cl_sj.sjb['cckcsj_czl'] = time.time()
                        cl_sj.sjb['zdbz_czl'] = cl_sj.sjb['zdbz_zl'] + 1
                        czlzdcgbz = False

        # 开仓标志复位
        if cl_sj.sjb['cczt_zl'] == 1 and cckcbz and zlzdcgbz:  # 主力开仓标志复位
            if cl_sj.sjb['xdid_zl'] in cl_sj.zhanghu[zhbh].order_dict:
                if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_zl']]['kcdsl'] == 0:
                    cl_sj.sjb['cczt_zl'] = 0
                    cl_sj.sjb['zdbz_zl'] = 0

        if cl_sj.sjb['cczt_czl'] == 1 and cckcbz and czlzdcgbz:  # 次主力开仓标志复位
            if cl_sj.sjb['xdid_czl'] in cl_sj.zhanghu[zhbh].order_dict:
                if cl_sj.zhanghu[zhbh].order_dict[cl_sj.sjb['xdid_czl']]['kcdsl'] == 0:
                    cl_sj.sjb['cczt_czl'] = 0
                    cl_sj.sjb['zdbz_czl'] = 0

    return cl_sj.sjb['cczt_zl'], cl_sj.sjb['cczt_czl'], cl_sj.sjb['zsl'], hycj
